﻿<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <script src="../Resource/JS/jquery-1.10.2.js"></script>
</head>
<body>
</body>
</html>
<script>
    var ajax = function (param) {
        var that = this;
        this.defaultParam = {
            url: "",
            data: null,
            dataType: "text"
        }
        this._d = null;
        this.extend = function (target, source) {
            for (var p in source) {
                if (source.hasOwnProperty(p)) {
                    target[p] = source[p];
                }
            }
            return target;
        }

        this.send = function () {
            this.defaultParam = this.extend(this.defaultParam, param);
            $.ajax({
                url: this.defaultParam.url,
                dataType: this.defaultParam.dataType,
                data: this.defaultParam.data,
                success: function (data) {
                    that._d = data;
                    that.dataArray.push(eval(data));
                }
            });
            return this;
        }

        this.then = function (fn) {
            var arg = arguments;
            if (this._d != null) {
                fn(this._d);
                console.log("Completed and arg is: " + arg[1]);
                return;
            }

            console.log("Not completed and arg is: " + arg[1]);
            var arg = arguments;
            window.setTimeout(function () { arg.callee.apply(that, arg) }, 10);
        }
    }

    ajax.prototype.dataArray = [];

    /*
        ajaxArr: ajaxArr为ajax对象数组
        callbackArr: callbackArr为回调数组
        callbackCompleted: 是否等待回调执行完毕在进行下一次发送请求
    */
    var ajaxPool = function (ajaxArr, callbackArr, callbackCompleted) {
        var that = this;
        this.ajaxArr = ajaxArr;
        this.callbackArr = callbackArr;
        this.isCallbackCompleted = callbackCompleted;

        this.do = function () {
            if (this.isCallbackCompleted) {
                that.completed();
            }
            else {
                that.inCompleted();
            }
        }

        var count = 0;
        this.completed = function () {
           
        }
        this.inCompleted = function () {
            (function () {
                if (count == that.ajaxArr.length) {
                    return;
                }
                var t = that.ajaxArr[count].send();
                console.log("Has been Sent And Index is: " + count);
                t.then(that.callbackArr[count], count + "aa");
                count++;
                var arg = arguments.callee;
                setTimeout(arg, 10);
            })();
        }
    }

    var a = new ajax({ url: "js1.js", dataType: "script" });
    var b = new ajax({ url: "WebForm1.aspx", data: { tag: "get", id: 1 } });
    var c = new ajax({ url: "WebForm1.aspx", data: { tag: "get", id: 2 } });
    var d = new ajax({ url: "WebForm1.aspx", data: { tag: "get", id: 3 } });
    var e = new ajax({ url: "js2.js", dataType: "script" });

    function f1(d) {
        var d = eval(d);
    }
    var sa = new ajaxPool([a, b, c, d, e], [f1, f1, f1, f1, f1], false);
    sa.do();
</script>
